package com.wll.okhttpcode.https;

/**
 * If you have any questions, you can contact by email { wangzhumoo@gmail.com}
 * @author 王诛魔 2018/1/10 下午5:07
 * 支持https的工具类
 */

public class HttpsUtils {
//    private static final String TAG = "OkHttp";
//
//    /**
//     * 创建一个空白的KeyStore
//     * @return KeyStore
//     * @throws GeneralSecurityException
//     */
//    private static KeyStore createEmptyKeyStore() throws GeneralSecurityException {
//        try {
//            KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
//            //此处密码随意,不会产生问题
//            keystore.load(null, "password".toCharArray());
//            return keystore;
//        } catch (IOException e) {
//            return null;
//        }
//    }
//
//
//    /**
//     * TrustManager  生成
//     * @return
//     * @throws GeneralSecurityException
//     */
//    private static TrustManager[] generateTrustManager() throws GeneralSecurityException {
//
//        //创建一个X.509格式的CertificateFactory
//        CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
//
//        Certificate ca;
//        //证书工厂根据证书文件的流生成证书Certificate
//        InputStream assets = MyApp.getContext().getResources().openRawResource(R.raw.rootca);
//        ca = certificateFactory.generateCertificate(assets);
//        Log.e(TAG, "trustManager: ca = " + ((X509Certificate) ca).getSubjectDN() );
//
//
//        //创建KeyStore，用来存储信任证书
//        KeyStore keyStor = createEmptyKeyStore();
//        keyStor.setCertificateEntry("ca",ca);
//
//
//        //创建一个默认类型的TrustManagerFactory
//        String defaultAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
//        TrustManagerFactory tmf = TrustManagerFactory.getInstance(defaultAlgorithm);
//
//        //用keyStore实例初始化TrustManagerFactory,此时TrustManagerFactory会信任我们保存的证书
//        tmf.init(keyStor);
//
//        //通过tmf获取TrustManager数组，TrustManager也会信任keyStor中的证书
//
//        return tmf.getTrustManagers();
//    }
//
//
//    /**
//     * 提供一个一个SSLSocketFactory
//     * @return SSLSocketFactory实例
//     */
//    public static SSLSocketFactory generateSSLSocketFactory()  {
//        try {
//            SSLContext sc = SSLContext.getInstance("TLS");
//            sc.init(null, generateTrustManager(), new SecureRandom());
//            Log.e(TAG, "generateSSLSocketFactory ");
//            return sc.getSocketFactory();
//        } catch (GeneralSecurityException e) {
//            return null;
//        }
//    }
}
